<template>
  <el-dialog
    title="管理字典项"
    :visible.sync="dialogFormVisible2"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :fullscreen="false"
  >
    <div>
      <el-button type="text" @click="createData">新增字典项</el-button>
    </div>
    <el-table :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">
            编辑
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="" align="center" width="100" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="text" size="mini" @click="confirmUpdate(row)" v-show="row.disabled">
            确认
          </el-button>
          <el-button size="mini" type="text" @click="cancelUpdate(row)" v-show="row.disabled">
            取消
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="key" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="value">
        <template slot-scope="scope">
          <el-input v-model="scope.row.name" :disabled="!scope.row.disabled"></el-input>
        </template>
      </el-table-column>

      <el-table-column align="center" label="额外信息">
        <template slot-scope="scope">
          <el-input v-model="scope.row.extra" :disabled="!scope.row.disabled"></el-input>
        </template>
      </el-table-column>

      <el-table-column align="center" label="顺序">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.order2" :min="1" :max="9999" label="顺序" :disabled="!scope.row.disabled"></el-input-number>
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible2 = false">
        关闭
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as apiAppDic from '@/api/app_dic'
import * as apiApp from '@/api/app'

export default {
  name: 'ManageDicItems',
  props: ['dialogFormVisible'],
  computed: {
    dialogFormVisible2: {
      get() {
        return this.dialogFormVisible
      },
      set(val) {
        // dialogFormVisible改变由父组件控制
        this.$emit('update:dialogFormVisible', val)
      }
    }
  },
  created() {
  },
  data() {
    return {
      parentId: undefined,
      list: null,
      total: 0,
      listQuery: { // 查询列表时的参数对象
        pageNumber: 1,
        pageSize: 50,
      },
    }
  },
  methods: {
    setParentId(parentId) {
      this.parentId = parentId
    },
    async getList() { // 获取列表数据
      const { content } = await apiAppDic.getDicItems(this.parentId, this.listQuery)
      this.list = content.list
      this.total = content.totalCounts
    },
    handleUpdate(row) {
      this.$set(row, 'disabled', true)
    },
    cancelUpdate(row) {
      //row.disabled = false
      this.$set(row, 'disabled', false)
    },
    // 真正修改一个字典项!!!!
    async confirmUpdate(row) {
      if (!row.name) {
        alert('value不能为空')
        return
      }
      const updateObj = {
        id: row.id,
        name: row.name,
        extra: row.extra,
        order2: row.order2
      }
      await apiAppDic.createOrUpdateDicItems(this.parentId, updateObj)
      this.$set(row, 'disabled', false)
      await this.getList()
    },
    // 新增字典项
    createData() {
      const newRow = {
        disabled: true,
        id: undefined,
        name: undefined,
        extra: undefined,
        order2: 9999
      }
      this.list.push(newRow)
    },
    handleDelete(index, row) {
      if (!row.id) {
        this.list.splice(index, 1)
        return
      }
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const deleteData = Object.assign({}, row) // copy obj
        apiAppDic.deleteAppDicItems(deleteData.id).then(() => {
          // 刷新列表数据
          this.getList()
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped>

</style>
